<?php

namespace plugin\colecron\app\admin\controller;

use app\common\controller\BaseAdminController;
use Carbon\Carbon;
use plugin\colecron\app\service\CronService;
use support\exception\BadException;
use support\Response;
use think\facade\Db;

class CronController extends BaseAdminController
{
    public function list()
    {
        $form = request()->getForm();
        $where = [];
        if ($form["name"]){
            $where[] = ['name','like',"%{$form["name"]}%"];
        }
        if ($form["start_time"] && $form["end_time"]){
            $where[] = ['create_time','between',[
                Carbon::parse($form["start_time"])->startOfDay()->getTimestamp(),
                Carbon::parse($form["end_time"])->endOfDay()->getTimestamp(),
            ]];
        }

        $result = CronService::crontabIndex([
            'limit' => 10,
            'page' => 1,
            'where' => $where
        ]);
        return json($result);
    }

    public function add()
    {
        $param = [
            "title" => input('title', ""),      //任务标题
            "type" => input('type', ""),        //	任务类型 (1 command, 2 class, 3 url, 4 eval ,5 shell)
            "rule" => input('rule', ""),        //	*/3 * * * * *	任务执行表达式
            "target" => input('target', ""),        // 调用任务字符串
            "parameter" => input('parameter', "{}"),      //{}	调用任务参数(url和eval无效)
            "remark" => input('remark', ""),        //备注
            "sort" => input('sort', ""),        //排序
            "status" => input('status', 1),        //状态[0禁用; 1启用]
            "singleton" => input('singleton', 1),      //是否单次执行 [0 是 1 不是]
        ];
         CronService::crontabCreate($param);
        return $this->success();
    }

    public function update()
    {
        $param = [
            "id" => input('id', ""),
            "title" => input('title', ""),      //任务标题
            "type" => input('type', ""),        //	任务类型 (1 command, 2 class, 3 url, 4 eval ,5 shell)
            "rule" => input('rule', ""),        //	*/3 * * * * *	任务执行表达式
            "target" => input('target', ""),        // 调用任务字符串
            "parameter" => input('parameter', "{}"),      //{}	调用任务参数(url和eval无效)
            "remark" => input('remark', ""),        //备注
            "sort" => input('sort', ""),        //排序
            "status" => input('status', 1),        //状态[0禁用; 1启用]
            "singleton" => input('singleton', 1),      //是否单次执行 [0 是 1 不是]
        ];
        if (!$param['id']){
            throw new BadException("id参数错误");
        }
        CronService::crontabUpdate($param);
        return $this->success();
    }

    public function changeStatus()
    {
        $param = [
            "id" => input('id', ""),
            "status" => input('status', 1),        //状态[0禁用; 1启用]
        ];
        if (!$param['id']){
            throw new BadException("id参数错误");
        }
        CronService::crontabUpdate($param);
        return $this->success();
    }

    public function remove()
    {
        $param = [
            "id" => input('id', ""),      //任务标题
        ];
        if (!$param['id']){
            throw new BadException("id参数错误");
        }
        CronService::crontabDelete($param);
        return $this->success();
    }

    /**
     * 重启定时任务
     * @return Response
     * @throws BadException
     */
     public function reload()
    {
        $param = [
            "id" => input('id', ""),
        ];
        if (!$param['id']){
            throw new BadException("id参数错误");
        }
        CronService::crontabReload($param);
        return $this->success();
    }

    /**
     * 重启定时任务
     * @return Response
     * @throws BadException
     */
     public function run()
    {
        $id =input('id', "");
        if (!$id){
            throw new BadException("id参数错误");
        }
        CronService::crontabRun($id);
        return $this->success();
    }
  /**
     * 重启定时任务
     * @return Response
     * @throws BadException
     */
     public function getById()
    {
        $res = Db::table("t_system_crontab")
            ->where([
                'id'=>input('id')
            ])
            ->find();
        return $this->success($res);
    }

}
